#include<stdio.h>
#include<stdlib.h>
#include<gui.h>
#include<gui_window.h>
#include<gui_text.h>
#include<gui_box.h>
#include<uview_keycode.h>
#include"edit_window.h"
#include"edit_ctrl.h"
#include"edit_debug.h"
#include<dotfont.h>
#include<assert.h>

edit_window_t edit_window;


static int edit_window_key_down_event(gui_spirit_t *spirit,gui_event_t *event,void *data)
{
    #if EDIT_WINDOW_EVENT_DEBUG==1
    dprintf("[edit window] key down: key %x mod %x\n",event->key.keycode.code,event->key.keycode.modify);
    #endif
    edit_ctrl_get_key(uview_keypad2ascii(event->key.keycode.code),event->key.keycode.modify);
}

static int edit_window_save_btn_event(gui_spirit_t *spirit)
{
    #if EDIT_WINDOW_EVENT_DEBUG==1
    dprintf("[edit window] save btn down\n");
    #endif
    //edit_save();
}

static int edit_window_open_btn_event(gui_spirit_t *spirit)
{
    #if EDIT_WINDOW_EVENT_DEBUG==1
    dprintf("[edit window] open btn down\n");
    #endif
}

static int edit_window_bar_init(edit_window_t *edit_window)
{
    gui_spirit_t *window=edit_window->edit_window;
    assert(window!=NULL);

    //create widget
    gui_spirit_t *box=gui_box_create(GUI_BOX_ORIENTATION_HORIZONTAL,window->width,45,2);
    if(!box)
    {
        dprintf("[debug] editor: box create error\n");
        return -1;
    }
    gui_spirit_t *save=gui_button_create_with_label("save");
    if(!save)
    {
        dprintf("[debug] editor: save button create err\n");
        return -1;
    }
    gui_spirit_t *open=gui_button_create_with_label("open");
    if(!save)
    {
        dprintf("[debug] editor: open button create err\n");
        return -1;
    }
    gui_spirit_t *cur_file=gui_label_create("NONE");
    if(!cur_file)
    {
        dprintf("[debug] editor: cur file label create err\n");
        return -1;
    }
    cur_file->style.font_align=GUI_ALIGN_CENTER;
    //set positon
    int x=0,y=0;
    gui_spirit_set_pos(box,0,0);
    gui_spirit_set_pos(save,GUI_BOX(box)->spacing,GUI_BOX(box)->spacing);
    x+=save->width+GUI_BOX(box)->spacing;
    gui_spirit_set_pos(open,x,y);
    x+=open->width+GUI_BOX(box)->spacing;
    gui_spirit_set_pos(cur_file,x,y);
    //add to container
    gui_container_add(GUI_CONTAINER(box),save);
    gui_container_add(GUI_CONTAINER(box),open);
    gui_container_add(GUI_CONTAINER(box),cur_file);
    gui_container_add(GUI_CONTAINER(window),box);
    gui_spirit_show_all(box);
}

int edit_window_init(int width,int height)
{
    if(gui_init(NULL,NULL)<0)
        return -1;
        
    gui_spirit_t *window=gui_window_create(GUI_WINDOW_TOPLEVEL);
    if(!window)
    {
        dprintf("[edit] create app window err\n");
        return -1;
    }
    
    gui_window_set_default_size(GUI_WINDOW(window),width,height);
    gui_window_set_title(GUI_WINDOW(window),"text editor");
    gui_window_set_position_absolute(GUI_WINDOW(window),0,0);

    edit_window.edit_window=window;
    edit_window.edit_surface=window->surface;

    gui_surface_rectfill(window->surface,0,0,width,height,EDIT_WINDOW_BACKCOLOR);
 
    gui_signal_connect(window,"key_press",edit_window_key_down_event,NULL); //input event handler 
    gui_window_show(window);

    //create editor function bar
    //edit_window_bar_init(&edit_window);

    return 0;
}


void edit_window_destroy(gui_window_t *window)
{
    gui_window_destroy(window);
}

int edit_window_rectfill(int x,int y,int w,int h,int color)
{
    gui_surface_t *surface=gui_surface_create(w,h);
    if(!surface)
        return -1;
    gui_surface_rectfill(surface,0,0,w,h,color);
    gui_rect_t rect={x,y,w,h};
    gui_surface_blit(surface,NULL,edit_window.edit_surface,&rect);

    gui_window_update(edit_window.edit_window,x,y,w,h);

    gui_surface_destroy(surface);
}

int edit_window_rect(int x,int y,int w,int h,gui_color_t color)
{
     gui_surface_t *surface=gui_surface_create(w,h);
    if(!surface)
        return -1;
    gui_surface_rect(surface,0,0,w,h,color);
    gui_rect_t rect={x,y,w,h};
    gui_surface_blit(surface,NULL,edit_window.edit_surface,&rect);

    gui_window_update(edit_window.edit_window,x,y,w,h);

    gui_surface_destroy(surface);
}


void edit_window_show_char(int x,int y,char ch,gui_color_t color)
{
    gui_surface_t *surface=gui_surface_create(edit_ctrl.char_width,edit_ctrl.char_height);
    if(!surface)
        return;
    char s[2]={0};
    s[0]=ch;
    gui_text_to_surface(s,color,DOTF_STANDARD_NAME,surface,0,0);
    gui_rect_t rect;
    gui_rect_init(&rect,x,y,surface->w,surface->h);
    gui_surface_blit(surface,NULL,edit_window.edit_surface,&rect);
    //no refresh,improve profit
    gui_window_update(GUI_WINDOW(edit_window.edit_window),x,y,surface->w,surface->h);

    gui_surface_destroy(surface);
}
 
void edit_window_update(int left,int top,int right,int bottom)
{
    gui_window_update(GUI_WINDOW(edit_window.edit_window),left,top,right-left,bottom-top);
}


int edit_window_get_size(int *width,int *height)
{
    if(!edit_window.edit_window)
        return -1;
        
    if(width)
        *width=edit_window.edit_window->width;
    if(height)
        *height=edit_window.edit_window->height;
    dprintf("[edit] get window w %d h %d\n",*width,*height);
    return 0;
}